<?php
declare (strict_types=1);

namespace app\common\queue\notify;

use app\common\library\Queue;
use app\course\model\base\StudentCourseNotify;
use think\facade\Request;

class CheckStudentCourseExpired
{
    public function run()
    {
        $data = Request::post();
        // 分页数
        $this->page = $data['page'] ?? 1;
        // 获取学员列表
        $users = StudentCourseNotify::where('expired_status', '<>', 2)->paginate(100);

        if ($users->isEmpty()) return;

        foreach ($users as $user) {
            // 延迟5秒执行
            Queue::push('SendStudentCourseExpiredNotify', [
                'user_id'   => $user->getAttr('user_id'),
                'course_id' => $user->getAttr('course_id')
            ], 5);
        }

        // 继续推送任务
        if ($users->hasPages()) {
            Queue::push('CheckStudentCourseExpired', [
                'page' => $this->page + 1
            ]);
        }
    }
}